package com.aiti.lulian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.page.PageInfo;
import com.aiti.base.core.page.PageResult;
import com.aiti.lulian.bo.LoginUserInfo;
import com.aiti.lulian.dto.industry.CountyParkParamDto;
import com.aiti.lulian.dto.industry.CountyParkQueryDto;
import com.aiti.lulian.entity.industry.CardGovCountyPark;
import com.aiti.lulian.mapper.card.industry.CardGovCountyParkMapper;
import com.aiti.lulian.service.CardGovCountyParkService;
import com.aiti.lulian.utils.BusinessUtil;
import com.aiti.lulian.vo.industry.CountyParkVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description: CardGovCountyParkServiceImpl
 * @auth fcb
 * @date 2025-03-07 09:10
 **/
@Service
@Slf4j
public class CardGovCountyParkServiceImpl extends BaseSerivceImpl<CardGovCountyParkMapper, CardGovCountyPark> implements CardGovCountyParkService {


    /**
     * 新增
     * @param paramDto
     * @param loginUserInfo
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void addCountyPark(CountyParkParamDto paramDto, LoginUserInfo loginUserInfo) {
        if(StrUtil.isEmpty(paramDto.getParkName()) || StrUtil.isEmpty(paramDto.getImageUrl())) {
            throw new BusinessException("参数错误");
        }
        Integer count = this.baseMapper.queryCountByName(paramDto.getParkName());
        if(count > 0) {
            throw new BusinessException("园区名称已存在");
        }
        CardGovCountyPark po = new CardGovCountyPark();
        po.setParkName(paramDto.getParkName());
        po.setBaseId(UUID.randomUUID().toString());
        po.setImageUrl(paramDto.getImageUrl());
        po.setParkPhone(paramDto.getParkPhone());
        po.setParkDescription(paramDto.getParkDescription());
        po.setBaseCreateBy(loginUserInfo.getUserId());
        this.save(po);
    }

    /**
     * 修改
     * @param paramDto
     * @param loginUserInfo
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void updateCountyPark(CountyParkParamDto paramDto, LoginUserInfo loginUserInfo) {
        if(StrUtil.isEmpty(paramDto.getBaseId())) {
            throw new BusinessException("参数错误");
        }
        CardGovCountyPark po = new CardGovCountyPark();
        BeanUtil.copyProperties(paramDto, po);
        po.setBaseUpdateBy(loginUserInfo.getUserId());

        this.baseMapper.updateCountyPark(po);
    }

    /**
     * 删除
     * @param baseIds
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void deleteCountyPark(List<String> baseIds) {
        if(CollectionUtil.isEmpty(baseIds)) {
            return;
        }
        this.baseMapper.deleteCountyPark(baseIds);
    }

    /**
     * 查询列表
     * @param queryDto
     * @return
     */
    @Override
    public PageResult<CountyParkVo> queryCountyParkList(CountyParkQueryDto queryDto) {
        BusinessUtil.checkPageParam(queryDto);
        IPage<CountyParkVo> page = new Page<>(queryDto.getPageNum(), queryDto.getPageSize());
        List<CountyParkVo> list = this.baseMapper.queryCountyParkList(page, queryDto);
        PageResult<CountyParkVo> pageResult = new PageResult<>(list, new PageInfo(queryDto.getPageNum(), queryDto.getPageSize()), page.getTotal());
        return pageResult;
    }

    /**
     * 详情
     * @param baseId
     * @return
     */
    @Override
    public CountyParkVo getCountyParkDetail(String baseId) {
        if(StrUtil.isEmpty(baseId)) {
            throw new BusinessException("参数错误");
        }
        CardGovCountyPark cardGovCountyPark = this.baseMapper.selectById(baseId);
        if(cardGovCountyPark == null || cardGovCountyPark.getIsDelete() == 1) {
            throw new BusinessException("数据不存在");
        }
        CountyParkVo vo = new CountyParkVo();
        BeanUtil.copyProperties(cardGovCountyPark, vo);
        return vo;
    }


}
